layui.define(['table', 'form'], function (exports) {
    var $ = layui.$
        , table = layui.table
        , form = layui.form;

    //分类管理
    table.render({
        elem: '#LAY-app-content-category'
        , url: 'article_category'
        , cols: [[
            {type: 'numbers', fixed: 'left'}
            , {field: 'id', width: 100, title: 'ID', sort: true}
            , {field: 'name', title: '分类名', minWidth: 100}
            , {field: 'sort', title: '排序'}
            , {field: 'created_at', title: '创建时间'}
            , {field: 'updated_at', title: '更新时间'}
            , {title: '操作', width: 250, align: 'center', fixed: 'right', toolbar: '#layuiadmin-app-cont-tagsbar'}
        ]]
        , text: '对不起，加载出现异常！'
    });

    //监听工具条
    table.on('tool(LAY-app-content-category)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除此分类？', function (index) {
                layer.confirm('确认删除吗？', function (index) {
                    $.post("{{ route('admin.category.destroy') }}", {
                        _method: 'delete',
                        ids: data.id
                    }, function (result) {
                        if (result.code === 0) {
                            obj.del(); //删除对应行（tr）的DOM结构
                        }
                        layer.close(index);
                        layer.msg(result.msg)
                    });
                });
            });
        } else if (obj.event === 'edit') {
            var tr = $(obj.tr);
            layer.open({
                type: 2
                , title: '编辑分类'
                , content: '/article_category/' + data.id + '/edit'
                , area: ['450px', '420px']
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    //获取iframe元素的值
                    var othis = layero.find('iframe').contents().find("#layuiadmin-app-form-tags")
                        , tags = othis.find('input[name="tags"]').val();

                    if (!tags.replace(/\s/g, '')) return;

                    obj.update({
                        tags: tags
                    });
                    layer.close(index);
                }
                , success: function (layero, index) {
                    //给iframe元素赋值
                    var othis = layero.find('iframe').contents().find("#layuiadmin-app-form-tags").click();
                    othis.find('input[name="tags"]').val(data.tags);
                }
            });
        }
    });

    //文章管理
    table.render({
        elem: '#LAY-app-content-list'
        , url: '/article'
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', width: 80, title: '文章ID', sort: true}
            , {field: 'category', title: '分类', toolbar: '#category'}
            , {field: 'title', title: '标题'}
            , {field: 'thumb', title: '缩略图', toolbar: '#thumb', width: 100}
            , {field: 'tags', title: '标签', toolbar: '#tags', width: 100}
            , {field: 'click', title: '点击量'}
            , {field: 'created_at', title: '创建时间'}
            , {field: 'updated_at', title: '更新时间'}
            , {field: 'status', title: '发布状态', templet: '#buttonTpl', minWidth: 80, align: 'center'}
            , {title: '操作', minWidth: 180, align: 'center', fixed: 'right', toolbar: '#table-content-list'}
        ]]
        , page: true
        , limit: 10
        , limits: [10, 15, 20, 25, 30]
        , text: '对不起，加载出现异常！'
    });

    //监听工具条
    table.on('tool(LAY-app-content-list)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除此文章？', function (index) {
                admin.req({
                    url: '/article/' + data.id,
                    type: "delete",
                    success: function (res) {
                        if (res.code === 0) {
                            obj.del();
                            layer.msg(res.message, {icon: 6});
                        } else {
                            layer.msg(res.message, {icon: 5});
                        }
                        layer.close(index);
                    }
                });
            });
        } else if (obj.event === 'edit') {
            const index = layer.open({
                type: 2
                , title: '编辑文章'
                , content: '/article/' + data.id + '/edit'
                , maxmin: true
                , area: ['550px', '550px']
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submit = layero.find('iframe').contents().find("#LAY-app-form-edit");

                    //监听提交
                    iframeWindow.layui.form.on('submit(LAY-app-form-edit)', function (data) {
                        var field = data.field; //获取提交的字段

                        $.ajax({
                            url: '/article/' + field.id,
                            data: field,
                            type: "put",
                            dataType: "json",
                            success: function (data) {
                                if (data.code === 0) {
                                    layer.msg(data.msg, {icon: 6});
                                    obj.update({
                                        label: field.label
                                        , title: field.title
                                        , author: field.author
                                        , status: field.status
                                    }); //数据更新
                                } else {
                                    layer.msg(data.msg, {icon: 5});
                                }
                            },
                            error: function (data) {
                                layer.msg(data.msg);
                            }
                        });
                        layer.close(index);
                    });

                    submit.trigger('click');
                }
            });
            layer.full(index);
        }
    });

    //评论管理
    table.render({
        elem: '#LAY-app-content-comm'
        , url: layui.setter.base + 'json/content/comment.js' //模拟接口
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', width: 100, title: 'ID', sort: true}
            , {field: 'reviewers', title: '评论者', minWidth: 100}
            , {field: 'content', title: '评论内容', minWidth: 100}
            , {field: 'commtime', title: '评论时间', minWidth: 100, sort: true}
            , {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-content-com'}
        ]]
        , page: true
        , limit: 10
        , limits: [10, 15, 20, 25, 30]
        , text: '对不起，加载出现异常！'
    });

    //监听工具条
    table.on('tool(LAY-app-content-comm)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除此条评论？', function (index) {
                obj.del();
                layer.close(index);
            });
        } else if (obj.event === 'edit') {
            layer.open({
                type: 2
                , title: '编辑评论'
                , content: '../../../views/app/content/contform.html'
                , area: ['450px', '300px']
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submitID = 'layuiadmin-app-comm-submit'
                        , submit = layero.find('iframe').contents().find('#' + submitID);

                    //监听提交
                    iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                        var field = data.field; //获取提交的字段

                        //提交 Ajax 成功后，静态更新表格中的数据
                        //$.ajax({});
                        table.reload('LAY-app-content-comm'); //数据刷新
                        layer.close(index); //关闭弹层
                    });

                    submit.trigger('click');
                }
                , success: function (layero, index) {

                }
            });
        }
    });

    //区块类型
    table.render({
        elem: '#LAY-app-content-block_type'
        , url: "block_type" //数据接口
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', width: 80, title: 'ID', sort: true}
            , {field: 'name', title: '类型名称', minWidth: 100}
            , {field: 'sort', title: '排序', minWidth: 100}
            , {field: 'created_at', title: '创建时间', minWidth: 100, sort: true}
            , {field: 'updated_at', title: '更新时间', minWidth: 100, sort: true}
            , {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-content-options'}
        ]]
        , page: true
        , limit: 10
        , limits: [10, 15, 20, 25, 30]
        , text: '对不起，加载出现异常！'
    });

    //监听工具条
    table.on('tool(LAY-app-content-block_type)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除此条类型？', function (index) {
                $.post("block_type/destroy", {_method: 'delete', ids: data.id}, function (res) {
                    if (res.code === 0) {
                        obj.del(); //删除对应行（tr）的DOM结构
                        layer.close(index);
                        layer.msg(data.msg, {icon: 6});
                    } else {
                        layer.msg(data.msg, {icon: 5});
                    }
                });
            });
        } else if (obj.event === 'edit') {
            layer.open({
                type: 2
                , title: '编辑类型'
                , content: 'block_type/' + data.id + '/edit'
                , area: ['450px', '300px']
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    //点击确认触发 iframe 内容中的按钮提交
                    var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                    submit.click();
                }
            });
        }
    });

    //区块管理
    table.render({
        elem: '#LAY-app-content-block'
        , url: "block"
        , cols: [[
            {checkbox: true, fixed: true}
            , {field: 'id', title: 'ID', sort: true, width: 60}
            , {field: 'block_type_id', title: '类型', toolbar: '#position'}
            , {field: 'name', title: '名称'}
            , {field: 'title', title: '标题'}
            , {field: 'image', title: '图片', toolbar: '#thumb'}
            , {field: 'url', title: '链接', width: 200}
            , {field: 'sort', title: '排序', width: 60}
            , {field: 'created_at', title: '创建时间'}
            , {field: 'updated_at', title: '更新时间'}
            , {fixed: 'right', title: '操作', width: 150, align: 'center', toolbar: '#options'}
        ]]
        , page: true
        , limit: 10
        , limits: [10, 15, 20, 25, 30]
        , text: '对不起，加载出现异常！'
    });

    //监听工具条
    table.on('tool(LAY-app-content-block)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除此条类型？', function (index) {
                $.post("block/destroy", {_method: 'delete', ids: data.id}, function (res) {
                    if (res.code === 0) {
                        obj.del(); //删除对应行（tr）的DOM结构
                        layer.close(index);
                        layer.msg(data.msg, {icon: 6});
                    } else {
                        layer.msg(data.msg, {icon: 5});
                    }
                });
            });
        } else if (obj.event === 'edit') {
            layer.open({
                type: 2
                , title: '编辑类型'
                , content: 'block/' + data.id + '/edit'
                , area: ['450px', '300px']
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    //点击确认触发 iframe 内容中的按钮提交
                    var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                    submit.click();
                }
            });
        }
    });

    exports('contlist', {})
});
